import { createStore, applyMiddleware, compose } from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import reducers from '../reducers';

let loggerMiddleware = createLogger();

const composedCreateStore = applyMiddleware(
    thunkMiddleware,
    loggerMiddleware
)(createStore);

function configureStore(initialState = {}) {
    const store = composedCreateStore(reducers, initialState);

    if(process.env.NODE_ENV=== 'development' && module.hot){
        module.hot.accept('../reducers', () => {
            const nextRootReducer = require('../reducers').default;
            store.replaceReducer(nextRootReducer);
        });
    }


    return store;
}

export default configureStore;